<?php

namespace App\Library\Rules;

use Illuminate\Contracts\Validation\Rule;

class MediasVideoRule extends BaseRule implements Rule
{
    private string $errorMessage = '媒体格式错误';

    /**
     * Desc:
     * @param string $attribute
     * @param mixed $value
     * @return boolean
     */
    public function passes($attribute, $value): bool
    {
        $this->errorMessage = $attribute . ' ' . $this->errorMessage;
        if (!is_array($value)) return false;
        foreach ($value as $v) {
            if (!is_string($v)) return false;
            else if (!preg_match(__('com.serialNumberReg'), $v)) return false;
            $mimeType = getCacheUploadFile($v);
            if (is_null($mimeType)) {
                $this->errorMessage = $attribute . ' 媒体不存在:' . $v;
                return false;
            } else if (!in_array($mimeType, __('mime_type.video'))) {
                return false;
            }
        }
        return true;
    }

    /**
     * Desc:
     * @return string
     */
    public function message(): string
    {
        return $this->errorMessage;
    }
}
